<template>
	<view class="wrap">
		<!-- 自定义导航栏 -->
		<u-navbar :autoBack="true" title="修改名字" :border-bottom="false">
			<view slot="right">
				<view class="submit-box">
					<u-button size="mini" :type="submitDisabled?'default':'primary'" :disabled="submitDisabled"
						@click="submit">
						<view class="submit-button-text">保存</view>
					</u-button>
				</view>
			</view>
		</u-navbar>
		<view class="input-box">
			<u-input class="input" v-model="name" type="text" :clearable="true" placeholder="开始取一个有趣的名字吧~"></u-input>
			<view class="input-number">
				<text :class="name.length>20?'error-text':''">{{name.length}}</text>/20
			</view>
		</view>

		<view class="tip-box">
			<u-icon class="tip-icon" name="error-circle-fill" size="28"></u-icon>
			<view>30天内允许修改三次</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				name: "",
				submitDisabled: true

			};
		},
		created() {
			this.name = this.vuex_user.name
		},
		watch: {
			name: {
				deep: true,
				immediate: true,
				handler(newVal, oldVal) {
					if (newVal.length > 0) {
						this.submitDisabled = false
					} else {
						this.submitDisabled = true
					}
				}
			}
		},
		methods: {
			submit() {
				if (this.name.length < 20) {
					const that = this
					uni.showModal({
						title: '提示',
						content: '确定修改名字？',
						success(res) {
							if (res.confirm) {
								uni.$emit('modifyName', that.name);
								let canNavBack = getCurrentPages();
								if (canNavBack && canNavBack.length > 1) {
									uni.navigateBack({
										delta: 1
									});
								} else {
									history.back();
								}
							}
						}
					})
				} else {
					uni.showToast({
						title: '名字只能10个中文，20个字符',
						icon: 'none'
					})
				}
			},
		}
	};
</script>
<style lang="scss" scoped>
	page {
		background: #fff;
	}

	.submit-box {
		margin-right: 20rpx;

		.submit-button-text {
			padding: 0rpx 20rpx;
			font-size: 13px;
			line-height: 13px;
		}
	}

	.input-box {
		display: flex;
		background: #F5F7F6;
		border-radius: 20rpx;
		margin: 20rpx;
		padding: 0rpx 20rpx;
		align-items: center;

		.input {
			margin-right: 10rpx;
			background: #F5F7F6;
			color: #000;
		}

		.input-number {
			color: #999;

			.error-text {
				color: #ff0000;
			}
		}
	}

	.tip-box {
		display: flex;
		color: #777777;
		margin: 20rpx;

		.tip-icon {
			margin-right: 5rpx;
		}
	}
</style>